#!/bin/bash

################################################################################
# Uninstall Everything - Complete Cleanup Script
# Removes Wine, Winetricks, QuickTime, Homebrew, and all related data
################################################################################

echo ""
echo "╔════════════════════════════════════════════════════════════╗"
echo "║          Uninstall Wine, Winetricks & Homebrew             ║"
echo "╚════════════════════════════════════════════════════════════╝"
echo ""
echo "This will completely remove:"
echo "  • Wine"
echo "  • Winetricks"
echo "  • Wine data (~/.wine) including QuickTime"
echo "  • Homebrew"
echo "  • All Homebrew packages"
echo ""
read -p "Continue? [y/N]: " -n 1 -r
echo ""

if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Cancelled."
    exit 0
fi

echo ""
echo "🗑️  Starting cleanup..."
echo ""

# Add Homebrew to PATH if it exists (for uninstall commands)
if [ -f "/opt/homebrew/bin/brew" ]; then
    eval "$(/opt/homebrew/bin/brew shellenv)"
elif [ -f "/usr/local/bin/brew" ]; then
    eval "$(/usr/local/bin/brew shellenv)"
fi

# 1. Uninstall Winetricks
if command -v brew &> /dev/null; then
    echo "Uninstalling Winetricks..."
    brew uninstall winetricks 2>/dev/null
    echo "✓ Winetricks uninstalled"
fi

# 2. Uninstall FFmpeg
if command -v brew &> /dev/null; then
    echo "Uninstalling FFmpeg..."
    brew uninstall ffmpeg 2>/dev/null
    echo "✓ FFmpeg uninstalled"
fi

# 2. Uninstall Wine (all versions)
if command -v brew &> /dev/null; then
    echo "Uninstalling Wine..."
    brew uninstall --cask wine-stable 2>/dev/null
    brew uninstall --cask wine-crossover 2>/dev/null
    brew uninstall --cask gcenx/wine/wine-crossover 2>/dev/null
    brew untap gcenx/wine 2>/dev/null
    echo "✓ Wine uninstalled"
else
    echo "⚠ Homebrew not found, skipping Wine uninstall"
fi

# 3. Remove Wine data (includes QuickTime and all Wine-installed apps)
if [ -d ~/.wine ]; then
    echo "Removing Wine data (includes QuickTime)..."
    rm -rf ~/.wine
    echo "✓ Wine data removed (QuickTime removed)"
fi

# 3.5 Remove Director64 data (includes serial number/license and game files)
if [ -d ~/.director64_data ]; then
    echo "Removing Director64 data (includes serial number and game files)..."
    rm -rf ~/.director64_data
    echo "✓ Director64 data removed"
fi

# 4. Remove Wine app if it exists
if [ -d "/Applications/Wine Stable.app" ]; then
    echo "Removing Wine Stable.app..."
    sudo rm -rf "/Applications/Wine Stable.app"
    echo "✓ Wine Stable.app removed"
fi

# 5. Remove Winetricks cache
if [ -d ~/.cache/winetricks ]; then
    echo "Removing Winetricks cache..."
    rm -rf ~/.cache/winetricks
    echo "✓ Winetricks cache removed"
fi

# 6. Remove any portable Wine installations
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ -d "$SCRIPT_DIR/PortableWine" ]; then
    echo "Removing portable Wine..."
    rm -rf "$SCRIPT_DIR/PortableWine"
    echo "✓ Portable Wine removed"
fi

if [ -d "$SCRIPT_DIR/PortableWine_arm64" ]; then
    rm -rf "$SCRIPT_DIR/PortableWine_arm64"
    echo "✓ Portable Wine (arm64) removed"
fi

if [ -d "$SCRIPT_DIR/PortableWine_x86_64" ]; then
    rm -rf "$SCRIPT_DIR/PortableWine_x86_64"
    echo "✓ Portable Wine (x86_64) removed"
fi

# 7. Uninstall Homebrew
if command -v brew &> /dev/null; then
    echo ""
    echo "Uninstalling Homebrew..."
    echo "(This will ask for confirmation)"
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
    echo "✓ Homebrew uninstalled"
else
    echo "⚠ Homebrew already uninstalled"
fi

# 8. Clean up Homebrew directories
echo ""
echo "Cleaning up Homebrew directories..."

if [ -d "/opt/homebrew" ]; then
    echo "Removing /opt/homebrew..."
    sudo rm -rf /opt/homebrew
    echo "✓ Removed /opt/homebrew"
fi

if [ -d "/usr/local/Homebrew" ]; then
    echo "Removing /usr/local/Homebrew..."
    sudo rm -rf /usr/local/Homebrew
    echo "✓ Removed /usr/local/Homebrew"
fi

# 9. Clean up shell profile
echo ""
echo "Cleaning up shell profile..."

if [ -f ~/.zprofile ]; then
    # Backup first
    cp ~/.zprofile ~/.zprofile.backup
    
    # Remove Homebrew lines
    sed -i '' '/homebrew/d' ~/.zprofile 2>/dev/null
    echo "✓ Cleaned ~/.zprofile (backup saved as ~/.zprofile.backup)"
fi

if [ -f ~/.bash_profile ]; then
    # Backup first
    cp ~/.bash_profile ~/.bash_profile.backup
    
    # Remove Homebrew lines
    sed -i '' '/homebrew/d' ~/.bash_profile 2>/dev/null
    echo "✓ Cleaned ~/.bash_profile (backup saved as ~/.bash_profile.backup)"
fi

echo ""
echo "╔════════════════════════════════════════════════════════════╗"
echo "║          ✅ Cleanup Complete!                              ║"
echo "╚════════════════════════════════════════════════════════════╝"
echo ""
echo "Everything has been removed:"
echo "  ✓ Wine uninstalled"
echo "  ✓ Winetricks uninstalled"
echo "  ✓ Wine data deleted (including QuickTime)"
echo "  ✓ Winetricks cache removed"
echo "  ✓ Portable Wine removed"
echo "  ✓ Homebrew uninstalled"
echo "  ✓ Homebrew directories cleaned"
echo "  ✓ Shell profiles cleaned"
echo ""
echo "💡 Next steps:"
echo "   1. Close and reopen Terminal (to reset environment)"
echo "   2. Test your app installation from scratch!"
echo ""
